package com.sketch.service;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.sketch.commom.PageResult;
import com.sketch.commom.Result;
import com.sketch.entity.SketchInfo;
import com.sketch.entity.Tag;
import com.sketch.entity.dto.SketchInfoDTO;
import com.sketch.entity.dto.TagDTO;
import com.sketch.mapper.TagMapper;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@Service
public class TagService {
    @Resource
    private TagMapper tagMapper;

    public Result add(TagDTO tagDTO){
        Tag tag = new Tag();
        BeanUtils.copyProperties(tagDTO,tag);
        tagMapper.insert(tag);
        return Result.success();
    }

    public Result list(Integer page, Integer size){

        Page<Tag> sketchInfoPage = new Page<>(page, size);
        IPage<Tag> result = tagMapper.selectPage(sketchInfoPage,null);
        PageResult dtoPage = new PageResult<>();
        BeanUtils.copyProperties(result,dtoPage);
        List<TagDTO> list = new ArrayList<>();
        if(ObjectUtils.isEmpty(result.getRecords())){
            return Result.success(dtoPage);
        }
        result.getRecords().forEach(e->{
            TagDTO TagDTO = new TagDTO();
            BeanUtils.copyProperties(e,TagDTO);
            list.add(TagDTO);
        });
        dtoPage.setList(list);
        return Result.success().withData(dtoPage);
    }
    public Result delete(Long id){
        tagMapper.deleteById(id);
        return Result.success();
    }

}
